package com.umeox.sdk_ring

import com.umeox.sdk_ring.core.RingCommandBuilder
import com.umeox.sdk_ring.protocol.IRingProtocol
import java.io.File
import java.util.*

class DefaultRingProtocolImpl(private val interaction: RingDeviceInteraction) : IRingProtocol {

    companion object {
        private const val TAG = "DefaultRingProtocolImpl"
    }

    /**
     * 指令生成器
     */
    private val commandBuilder = RingCommandBuilder()

    override fun disConnectDevice() {
        interaction.disConnectDevice()
    }

    override fun connectDevice() {
        interaction.connectDevice()
    }

    override fun directConnectDevice() {
        interaction.directConnectDevice()
    }

    override fun syncCurrentTimeAndTimeFormat(
        date: Date?,
        is24Hour: Boolean?,
        showTime: Boolean,
        timeZone: Double
    ) {
        interaction.writeCommand(
            commandBuilder.buildP01Command(date ?: Date(), is24Hour, showTime, timeZone),
            "发送同步当前时间指令"
        )
    }

    override fun getDeviceBatteryPower() {
        interaction.writeCommand(commandBuilder.buildP02Command(), "发送获取设备电量信息指令")
    }

    override fun flipDeviceScreen() {
        interaction.writeCommand(commandBuilder.buildP03Command(), "发送翻转屏幕指令")
    }

    override fun startFindDevice() {
        interaction.writeCommand(commandBuilder.buildP04Command(true), "发送开始查找手机指令")
    }

    override fun stopFindDevice() {
        interaction.writeCommand(commandBuilder.buildP04Command(false), "发送停止查找手机指令")
    }

    override fun getDeviceState() {
        interaction.writeCommand(commandBuilder.buildP05Command(), "发送获取设备状态指令")
    }

    override fun getPrayerCountInformation(tick: String) {
        interaction.writeCommand(
            commandBuilder.buildP06Command(tick),
            "发送获取指定tick值的计数信息指令"
        )
    }

    override fun getRealTimePrayerCount(tick: String) {
        interaction.writeCommand(commandBuilder.buildP07Command(tick), "发送获取实时计数数据指令")
    }

    override fun shutdown() {
        interaction.writeCommand(commandBuilder.buildP08Command(), "发送远程关机指令")
    }

    override fun setPrayerRemindersState(
        isOpen: Boolean,
        calculationMethodPosition: Int,
        juristicMethodPosition: Int,
        latitude: Double,
        longitude: Double,
        timeZone: Double
    ) {
        interaction.writeCommand(
            commandBuilder.buildP09Command(
                isOpen, calculationMethodPosition,
                juristicMethodPosition, latitude, longitude, timeZone
            ), "发送设置常规惯例指令"
        )
    }

    override fun reboot(macAddress: String) {
        interaction.writeCommand(commandBuilder.buildP10Command(macAddress), "发送重启指令")
    }

    override fun setPrayerReminders(
        isOpen: Boolean,
        startTime: String,
        endTime: String,
        intervalTime: Int
    ) {
        interaction.writeCommand(
            commandBuilder.buildP11Command(
                isOpen,
                startTime,
                endTime,
                intervalTime
            ), "发送设置诵经提醒指令"
        )
    }

    override fun getDeviceCacheData() {
        interaction.writeCommand(
            commandBuilder.buildP12Command(),
            "发送获取设备端所有已存未上传计数数据指令"
        )
    }

    override fun getDeviceConfigInfo() {
        interaction.writeCommand(commandBuilder.buildP13Command(), "发送获取设备端设置信息指令")
    }

    override fun getDeviceLog() {
        interaction.writeCommand(commandBuilder.buildP14Command(), "发送获取设备端相关log信息指令")
    }

    override fun setDeviceScreenBrightness(brightness: Int) {
        interaction.writeCommand(commandBuilder.buildP15Command(brightness), "发送设置屏幕亮度指令")
    }

    override fun setDeviceLanguage(languageType: Int) {
        interaction.writeCommand(
            commandBuilder.buildP16Command(languageType),
            "发送设置戒指语言指令"
        )
    }

    override fun setCustomizeConvention(
        fajrAngle: Float,
        maghribIsMinutes: Boolean,
        maghribValue: Float,
        ishaIsMinutes: Boolean,
        ishaValue: Float
    ) {
        interaction.writeCommand(
            commandBuilder.buildP17Command(
                fajrAngle,
                maghribIsMinutes,
                maghribValue,
                ishaIsMinutes,
                ishaValue
            ), "发送设置戒指自定义惯例指令"
        )
    }

    override fun setStableConvention(index: Int) {
        interaction.writeCommand(commandBuilder.buildP18Command(index), "发送设置特殊祈祷指令")
    }

    override fun setTasbihTask(enable: Boolean, targetNumber: Int, taskId: Int) {
        interaction.writeCommand(
            commandBuilder.buildP19Command(enable, targetNumber, taskId),
            "发送设置赞念任务指令"
        )
    }

    override fun getTasbihTaskInfo(tick: String) {
        interaction.writeCommand(commandBuilder.buildP20Command(tick), "发送获取赞念任务信息指令")
    }

    override fun setShipNavigationMode() {
        interaction.writeCommand(commandBuilder.buildP21Command(), "发送设置船航模式指令")
    }

    override fun setScreenRestTime(time: Int) {
        interaction.writeCommand(commandBuilder.buildP22Command(time), "发送设置息屏时间指令")
    }

    override fun setPrayerRemindConfig(configs: Array<Array<Int>>) {
        interaction.writeCommand(commandBuilder.buildP23Command(configs), "发送设置息屏时间指令")
    }

    override fun setCustomCountReminder(normal33: Boolean, normal100: Boolean, customValue: Int) {
        interaction.writeCommand(
            commandBuilder.buildP24Command(normal33, normal100, customValue),
            "发送设置自定义计数提醒指令"
        )
    }

    override fun getPrayerRemindConfig() {
        interaction.writeCommand(commandBuilder.buildP25Command(), "发送获取祈祷提醒配置信息指令")
    }

    override fun setUserInfo(
        age: Int,
        gender: Int,
        height: Int,
        weight: Int,
        hrMax: Int,
        hrRest: Int,
        vo2max: Int
    ) {
        interaction.writeCommand(
            commandBuilder.buildP26Command(
                age,
                gender,
                height,
                weight,
                hrMax,
                hrRest,
                vo2max
            ), "发送设置用户信息指令"
        )
    }

    override fun getCurrentStepNumber() {
        interaction.writeCommand(commandBuilder.buildP27Command(), "发送获取当前步数信息指令")
    }

    override fun getAllSleepData() {
        interaction.writeCommand(commandBuilder.buildP28Command(), "发送获取历史睡眠数据指令")
    }

    override fun getAllStepData() {
        interaction.writeCommand(commandBuilder.buildP29Command(), "发送获取历史步数数据指令")
    }

    override fun pkeyOperation(write: Boolean, pkey: String) {
        interaction.writeCommand(
            commandBuilder.buildP30Command(write, pkey),
            "发送操作pkey值数据指令"
        )
    }

    override fun n01ScreenTest(light: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP31Command(light),
            "发送全亮测试指令(N01)数据指令"
        )
    }

    override fun gsensorTest() {
        interaction.writeCommand(
            commandBuilder.buildP32Command(),
            "发送Gsensor 测试数据指令"
        )
    }

    override fun setTimeShowType(type: Int) {
        interaction.writeCommand(
            commandBuilder.setTimeShowType(type),
            "发送时间显示类型设定数据指令"
        )
    }

    override fun getWorshipCacheData(tick: String) {
        interaction.writeCommand(
            commandBuilder.buildP34Command(tick),
            "朝拜计数信息获取"
        )
    }

    override fun getRealtimeWorship() {
        interaction.writeCommand(
            commandBuilder.buildP35Command(),
            "发送实时朝拜计数接收数据指令"
        )
    }

    override fun setLedTimeAndLight(lightTime: Int, light: Int) {
        interaction.writeCommand(
            commandBuilder.buildP36Command(lightTime,light),
            "发送led灯时间及亮度设定数据指令"
        )
    }

    override fun getSc01DeviceInfo() {
        interaction.writeCommand(
            commandBuilder.buildP37Command(),
            "发送设备信息获取数据指令"
        )
    }

    override fun deviceInfraRedTest(enable: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP38Command(enable),
            "发送设备红外测试数据指令"
        )
    }

    override fun deviceRadarTest(enable: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP39Command(enable),
            "发送设备雷达测试数据指令"
        )
    }

    override fun getAllWorshipCacheData() {
        interaction.writeCommand(
            commandBuilder.buildP40Command(),
            "发送获取设备端所有已存未上传朝拜计数数据数据指令"
        )
    }

    override fun modifyWorshipNumberByIndex(index: Int, number: Int) {
        interaction.writeCommand(
            commandBuilder.buildP41Command(index, number),
            "发送朝拜计数指令数据指令"
        )
    }

    override fun deviceMotorAgingTest(enable: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP42Command(enable),
            "发送设备马达老化测试数据指令"
        )
    }

    override fun getDeviceMotorAgingTestResul() {
        interaction.writeCommand(
            commandBuilder.buildP43Command(),
            "发送获得设备雷达返回触发结果反馈(设备雷达才会退出测试模式)数据指令"
        )
    }

    override fun getTofResult() {
        interaction.writeCommand(
            commandBuilder.buildP44Command(),
            "发送获取tof数据反馈数据指令"
        )
    }

    override fun tofCalibration1() {
        interaction.writeCommand(
            commandBuilder.buildP45Command(),
            "发送tof校准指令1数据指令"
        )
    }

    override fun tofCalibration2() {
        interaction.writeCommand(
            commandBuilder.buildP46Command(),
            "发送tof校准指令2数据指令"
        )
    }

    override fun sc01SpeakerAudioTest(audio1Enable: Boolean, audio2Enable: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP47Command(audio1Enable,audio2Enable),
            "发送喇叭音频测试指令"
        )
    }

    override fun setSc01AudioEnable(audioEnable: Boolean) {
        interaction.writeCommand(
            commandBuilder.buildP51Command(audioEnable),
            "发送音频播放设定指令"
        )
    }

    override fun startOTAUpgrade(
        file: File,
        isSafeOTA: Boolean,
        listener: RingOTAUpgradeListener
    ) {
        interaction.startOTA(file, isSafeOTA, listener)
    }

    override fun startHDOTAUpgrade(file: File, listener: RingOTAUpgradeListener) {
        interaction.startHDOTA(file, listener)
    }

}